<?php 
	//require('../../Smarty/libs/Smarty.class.php');
	require('../../Smarty/libs/SmartyBC.class.php');
	
	$smarty = new Smarty;

	$smarty->setTemplateDir('./smarty/templates/');
	$smarty->setCompileDir('./smarty/templates_c/');
	$smarty->setConfigDir('./smarty/configs/');
	$smarty->setCacheDir('./smarty/cache/');
	
?>
<?php
        include_once 'API/APIObject.php';
        include_once 'API/Code/Filter.php';
        include_once 'API/Code/RoomGroup.php';

        // POST Variables
        $destination = urlencode($_POST["destination"]);
        $checkIn = $_POST["check-in"];
        $checkOut = $_POST["check-out"];
        $countryCode = NULL;//$_POST["country-code"];
        $numberOfResults = 10/*$_POST["number-of-results"]*/;
        $stateProvince = NULL;
        $rooms[1] = $_POST["room1"];
        $rooms[2] = $_POST["room2"];
        $rooms[3] = $_POST["room3"];
        $rooms[4] = $_POST["room4"];
        $rooms[5] = $_POST["room5"];
        $rooms[6] = $_POST["room6"];
        $rooms[7] = $_POST["room7"];        
        $rooms[8] = $_POST["room8"];
        $roomCount = $_POST["room_noumber"];
        // Variables    
        $options = "DEFAULT";
        $optimizationState = NULL;
        		echo "Sunt aici.";
        // API Section
        $api = new APIObject();
        //$api->PrintTestDataOnScreen($_POST, "POST DATA");
        /*$serverRes = $api->PingServer("qwerty");*/

        $filter = new Filter();
        $filter->supplierType = "E";
        $filter->RoomGroup = new RoomGroup($rooms, $roomCount);
        $hotelRes = $api->GetHotelList_City($checkIn,$checkOut,$numberOfResults,
                $destination,$stateProvince,$countryCode,$filter,$options,
                $optimizationState);
        //$api->PrintTestDataOnScreen($hotelRes, "Server Response: ");
        
        
        // Smarty Section
        $smartyData = $smarty->createData();
        $hotelArray = array();
        //$api->PrintTestDataOnScreen($hotelRes, "Hotel Res");
        for ($index = 0; $index < count($hotelRes->HotelList->HotelSummary); $index++) 
        {
            $hotelArray[$index]["thumbnailUrl"] = "http://images.travelnow.com".$hotelRes->HotelList->HotelSummary[$index]->thumbNailUrl;
            $hotelArray[$index]["hotelName"] = $hotelRes->HotelList->HotelSummary[$index]->name;
            $hotelArray[$index]["shortDesc"] = $hotelRes->HotelList->HotelSummary[$index]->shortDescription;
            $hotelArray[$index]["rating"] = $hotelRes->HotelList->HotelSummary[$index]->hotelRating;
            $hotelArray[$index]["supplierType"] = $hotelRes->HotelList->HotelSummary[$index]->supplierType;
            /*$hotelArray[$index]["rateKey"] = 
            $hotelRes->HotelList->HotelSummary[$index]->RoomRateDetailsList->RoomRateDetails[0]->rateKey;*/
            
            // PUFFY
            $hotelArray[$index]["lRate"] = $hotelRes->HotelList->HotelSummary[$index]->lowRate;
            $hotelArray[$index]["hRate"] = $hotelRes->HotelList->HotelSummary[$index]->highRate;

            // Get hotelId
            $hotelId = $hotelRes->HotelList->HotelSummary[$index]->hotelId;
            $hotelArray[$index]["hotelId"] = $hotelId;
            
            $hotelInfo = $api->GetHotelInformation($hotelId, "DEFAULT");
            // Get hotel information
            //$api->PrintTestDataOnScreen($hotelId, "HOTEL ID");
            $hotelArray[$index]["propertyDetails"] = html_entity_decode($hotelInfo->HotelDetails->propertyDescription);
            $hotelArray[$index]["id"] = $index;
            
            $roomTypes = $hotelInfo->RoomTypes->RoomType;
            
            for ($index2 = 0; $index2 < 2; $index2++) {
                $hotelArray[$index]["roomTypes"][$index2]["description"] = 
                    $roomTypes[$index2]->description;
                $hotelArray[$index]["roomTypes"][$index2]["descriptionLong"] = 
                    $roomTypes[$index2]->descriptionLong;
            }
            
            $hotelImages = $hotelInfo->HotelImages->HotelImage;
            $hotelArray[$index]["nrOfPictures"] = count($hotelImages);
            
            for ($index2 = 0; $index2 < count($hotelImages); $index2++) {
                $hotelArray[$index]["hotelImages"][$index2]["thumbUrl"] = 
                $hotelImages[$index2]->thumbnailUrl;
            }

            for ($index2 = 0; $index2 < count($hotelImages); $index2++) {
                $hotelArray[$index]["hotelImages"][$index2]["url"] = $hotelImages[$index2]->url;
                $hotelArray[$index]["hotelImages"][$index2]["nr_picture"] = $index2;
            } 
            //////////////////////////////////////////////////////////////
            // Get Room Availability
            $roomAvail = $api->GetRoomAvailability($hotelId,$checkIn,$checkOut,NULL,
                    $hotelRes->HotelList->HotelSummary[$index]->supplierType, 
                    $hotelRes->HotelList->HotelSummary[$index]->
                    RoomRateDetailsList->RoomRateDetails->rateKey,
                    $hotelRes->HotelList->HotelSummary[$index]->
                    RoomRateDetailsList->RoomRateDetails->rateCode,
                    $hotelRes->HotelList->HotelSummary[$index]->
                    RoomRateDetailsList->RoomRateDetails->roomTypeCode,
                    TRUE,TRUE,"HOTEL_DETAILS,ROOM_TYPES,ROOM_AMENITIES,PROPERTY_AMENITIES,HOTEL_IMAGES",
                    $filter->RoomGroup
                    );
            
            //$api->PrintTestDataOnScreen($roomAvail, "RoomAvail");
                /*$hotelArray[$index]["rateKey"] = $roomAvail->rateKey;
                $hotelArray[$index]["roomTypeCode"] = $roomAvail->roomTypeCode;
                $hotelArray[$index]["rateCode"] = $roomAvail->rateCode;*/
                /*for ($index2 = 0; $index2 < count($roomAvail->RoomGroup->Room); $index2++) {
                    $hotelArray[$index]["roomTypes"][$index2]["chargeableRate"] = $roomAvail->RoomGroup->Room[$index2]->chargeableRate;
                } */

        }
		
        $smartyData->assign("hotelArray",$hotelArray);
        $smarty->display("hotelsList.tpl",$smartyData);
        //$api->PrintTestDataOnScreen($hotelInfo, "Hotel info");
        
        // Session Section
        $_SESSION["roomsNumber"] = $roomCount;
        for($index=1; $index<$roomCount; $index++)
        {
            $_SESSION["roomsData"][$index]["adults"]=$rooms[$index]["adultsCount"];
            $_SESSION["roomsData"][$index]["childrenNr"]=$rooms[$index]["childrenCount"];
            $_SESSION["roomsData"][$index]["childrenAge"]=
                    $rooms[$index]["children1"].",".
                    $rooms[$index]["children1"].",".
                    $rooms[$index]["children1"];
        }
        $_SESSION["checkIn"]=$checkIn;
        $_SESSION["checkOut"]=$checkOut;

?>
